<!--/*
* Copyright (c) 2025 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/-->

<html>
<head>
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <script>
        function textPostXhr(url) {
            let formData = new FormData();
            let myBlob = new Blob(['This is my blob content'], {type : 'text/plain'});
            formData.append('upload', myBlob);
            let xhr = new XMLHttpRequest();
            xhr.open('POST', url, true);
            xhr.send(formData);
            xhr.onreadystatechange = function (err) {
                console.log(err.target.status);
            };
        }
        function textPutXhr(url) {
            let formData = new FormData();
            let myBlob = new Blob(['This is my blob content'], {type : 'text/plain'});
            formData.append('upload', myBlob);
            let xhr = new XMLHttpRequest();
            xhr.open('PUT', url, true);
            xhr.send(formData);
            xhr.onreadystatechange = function (err) {
                console.log(err.target.status);
            };
        }
        function textPost(url, dataType) {
            let xhr = new XMLHttpRequest();
            xhr.open('POST', url, true);
            if (dataType === 'TEXT') {
                xhr.setRequestHeader('Content-Type', 'text/plain');
                let textValue = 'text@plain#ABC#abc@123';
                xhr.send(textValue);
            } else if (dataType === 'BYTES') {
                xhr.setRequestHeader('Content-Type', 'application/octet-stream');
                let byteArray = [104, 101, 108, 108, 111];
                let byteData = new Blob([new Uint8Array(byteArray)]);
                xhr.send(byteData);
            } else if (dataType === 'BLOB') {
                let formData = new FormData();
                let myBlob = new Blob(['This is my blob dataThis is my blob dataThis is my blob dataThis is my blob dataThis is my blob dataThis is my blob dataThis is my blob dataThis is my blob dataThis is my blob dataThis is my blob dataThis is my blob dataThis is my blob dataThis is my blob data'], {type : 'text/plain'});
                formData.append('blob', myBlob);
                xhr.send(formData);
            } else if (dataType === 'ARRAYBUFFER') {
                xhr.setRequestHeader('Content-Type', 'application/octet-stream');
                let arrayBuffer = new ArrayBuffer(15);
                const uint8Array = new Uint8Array(arrayBuffer);
                uint8Array[0] = 65;
                uint8Array[1] = 104;
                uint8Array[2] = 101;
                uint8Array[3] = 108;
                uint8Array[4] = 111;
                uint8Array[5] = 111;
                uint8Array[6] = 111;
                uint8Array[7] = 111;
                uint8Array[8] = 70;
                uint8Array[9] = 69;
                uint8Array[10] = 68;
                uint8Array[11] = 67;
                uint8Array[12] = 66;
                xhr.send(uint8Array.buffer);
            } else if (dataType === 'DOCUMENTOBJECT') {
                xhr.setRequestHeader('Content-Type', 'text/html');
                let testHtml = '<html><body bgcolor=\'white\'>abcdef12query</body></html>';
                xhr.send(testHtml);
            } else if (dataType === 'LT_BUFFER') {
                xhr.setRequestHeader('Content-Type', 'application/octet-stream');
                let byteArray = [104, 101, 108];
                let byteData = new Blob([new Uint8Array(byteArray)]);
                xhr.send(byteData);
            } else if (dataType === 'EQ_BUFFER') {
                xhr.setRequestHeader('Content-Type', 'application/octet-stream');
                let byteArray = [104, 101, 108, 108, 111, 104, 101, 108, 108, 111];
                let byteData = new Blob([new Uint8Array(byteArray)]);
                xhr.send(byteData);
            } else if (dataType === 'GT_BUFFER') {
                xhr.setRequestHeader('Content-Type', 'application/octet-stream');
                let byteArray = [104, 101, 108, 108, 111, 119, 111, 114, 100, 104, 101, 108];
                let byteData = new Blob([new Uint8Array(byteArray)]);
                xhr.send(byteData);
            }

            xhr.onreadystatechange = function () {
                console.log(xhr.status);
                console.log(xhr.getAllResponseHeaders());
                console.log(xhr.getResponseHeader('Content-Type'));
                if (xhr.readyState === 4) {
                    if (xhr.status === 0 || xhr.status === 200) {
                        console.log(xhr.responseText);
                        document.getElementById('response_content').innerText = xhr.responseText;
                    }
                }
            };
        }

        function testPostChunked(url) {
            let uploaded = 0;
            let buf = new Uint8Array(1024 * 50);
            let start = Date.now();
            let rs = new ReadableStream({
                    pull(ctrl) {
                    uploaded += buf.byteLength;
                    crypto.getRandomValues(buf);
                    ctrl.enqueue(buf);
                    if ((start + 1000) < Date.now()) {
                        ctrl.close();
                    }
                }
            });
            fetch(url, {
                method: 'POST',
                body: rs,
                duplex: 'half'
            }).then(response => {
                if (response.ok) {
                    return 'ok';
                }
                return '';
            }).then(data => {
                console.log(data);
                document.getElementById('response_content').innerText = data;
            }).then(error => {
                document.getElementById('response_content').innerText = error;
            });
        }

    </script>
</head>
<body>
<button onclick="textPostXhr('https://www.example.com/xhr')">test xhr post</button>
<button onclick="textPutXhr('https://www.example.com/xhr')">test xhr put</button>
<button onclick="textPost('https://www.example.com/xhr','TEXT')">test post TEXT</button>
<button onclick="textPost('https://www.example.com/xhr','BYTES')">test post BYTES</button>
<button onclick="textPost('https://www.example.com/xhr','BLOB')">test post BLOB</button>
<button onclick="textPost('https://www.example.com/xhr','ARRAYBUFFER')">test post ARRAYBUFFER</button>
<button onclick="textPost('https://www.example.com/xhr','DOCUMENTOBJECT')">test post DOCUMENTOBJECT</button>
<button onclick="textPost('https://www.example.com/xhr','LT_BUFFER')">test post LT_BUFFER</button>
<button onclick="textPost('https://www.example.com/xhr','EQ_BUFFER')">test post EQ_BUFFER</button>
<button onclick="textPost('https://www.example.com/xhr','GT_BUFFER')">test post GT_BUFFER</button>
<button onclick="testPostChunked('https://www.example.com/xhr')">test post chunked h</button>
<button onclick="textPost('https://www.example.com/httpBodyStreamNullptr','GT_BUFFER')">test post httpBodyStream Nullptr</button>
<button onclick="textPost('https://www.example.com/bufLenZero','GT_BUFFER')">test post bufLen Zero</button>

</body>
</html>